#include <gtest/gtest.h>
#include <Message.pb.h>
#include <fmtlog/fmtlog.h>
#include <MQ_Virtual_Engine/QueueMessageManage.hpp>
#include <Common/FileHelper.hpp>
#include <Common/UUIDHelper.hpp>
#include <filesystem>
#include <fstream>

class MessageManagerTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 每次测试前确保临时目录是干净的
        std::filesystem::remove_all(testDir);
        std::filesystem::create_directories(testDir);
    }

    void TearDown() override {
        // 测试完成后清理测试文件
        std::filesystem::remove_all(testDir);
    }

    const std::string testDir = "./test_mq";
};

TEST_F(MessageManagerTest, InitQueueMessage) {
    MY_MQ::MessageQueueManager manager(testDir);
    std::string queueName = "test_queue";

    // 初始化队列消息
    manager.initQueueMessage(queueName);

    // 检查是否成功初始化
    EXPECT_EQ(manager.Total_count(queueName), 0);
}

TEST_F(MessageManagerTest, InsertAndFrontMessage) {
    MY_MQ::MessageQueueManager manager(testDir);
    std::string queueName = "test_queue";
    manager.initQueueMessage(queueName);

    // 构造消息体和属性
    MQ_Protobuf::BasicProperties bp;
    bp.set_id(MY_MQ:: UUIDHelper::uuidWithShuffledMAC());
    bp.set_routing_key("test_key");
    bp.set_delivery_mode(MQ_Protobuf::DURABLE);

    std::string body = "Hello, MQ!";
    bool queueIsDurable = true;

    // 插入消息
    //---------------------------------
    //这里面存在指针无效问题
    bool insertSuccess = manager.insert(queueName, bp, body, queueIsDurable);
    EXPECT_TRUE(insertSuccess);

    // 获取消息
    auto msg = manager.front(queueName);
    ASSERT_NE(msg, nullptr);
    EXPECT_EQ(msg->payload().body(), body);
    EXPECT_EQ(msg->payload().properties().routing_key(), "test_key");
}

TEST_F(MessageManagerTest, ConfirmMessage) {
    MY_MQ::MessageQueueManager manager(testDir);
    std::string queueName = "test_queue";
    manager.initQueueMessage(queueName);

    // 构造消息体和属性
    MQ_Protobuf::BasicProperties bp;
    bp.set_id(MY_MQ:: UUIDHelper::uuidWithShuffledMAC());
    bp.set_routing_key("test_key_confirm");
    bp.set_delivery_mode(MQ_Protobuf::DURABLE);

    std::string body = "This is a test message";
    bool queueIsDurable = true;

    // 插入消息
    bool insertSuccess = manager.insert(queueName, bp, body, queueIsDurable);
    EXPECT_TRUE(insertSuccess);

    // 获取消息
    auto msg = manager.front(queueName);
    ASSERT_NE(msg, nullptr);

    // 确认消息
    manager.Confirm(queueName, msg->payload().properties().id());

    // 检查队列是否为空
    EXPECT_EQ(manager.ToBePushedMsg_Count(queueName), 0);
}

TEST_F(MessageManagerTest, GarbageCollection) {
    MY_MQ::MessageQueueManager manager(testDir);
    std::string queueName = "test_queue";
    manager.initQueueMessage(queueName);

    // 插入多条消息
    for (int i = 0; i < 10; ++i) {
        MQ_Protobuf::BasicProperties bp;
        bp.set_id(MY_MQ::UUIDHelper::uuidWithShuffledMAC());
        bp.set_routing_key("gc_key_" + std::to_string(i));
        bp.set_delivery_mode(MQ_Protobuf::DURABLE);

        std::string body = "Message " + std::to_string(i);
        bool queueIsDurable = true;

        bool insertSuccess = manager.insert(queueName, bp, body, queueIsDurable);
        EXPECT_TRUE(insertSuccess);
    }

    // 检查总数
    EXPECT_EQ(manager.Total_count(queueName), 10);

}

TEST_F(MessageManagerTest, ClearQueue) {
    MY_MQ::MessageQueueManager manager(testDir);
    std::string queueName = "test_queue";
    manager.initQueueMessage(queueName);

    // 插入消息
    std::string body = "Message to be cleared";
    bool insertSuccess = manager.insert(queueName, body, true);
    EXPECT_TRUE(insertSuccess);

    // 清理队列
    manager.destroyQueueMessage(queueName);
    // 检查是否成功清理
    EXPECT_EQ(manager.Total_count(queueName), 0);
}

class Init
{
    public:
        Init()
        {
            fmtlog::setHeaderPattern("{s} \t| {t} \t| {l} \t| {YmdHMS} \t| {g} \t| \n\t:");
            fmtlog::startPollingThread(12);
        }
};
int main(int argc, char **argv) {
    Init _____DONT_USE_ME__ONLY_INIT_LOG_____;
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}